package com.pcbly.web.components.form;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.pcbly.web.components.utils.LightsoftStringUtils;

public class AnalyseCommonDataExt {
	private String tableName;

	private String id;

	private String feedbackKeyAndValue;

	private final Log log = LogFactory.getLog(getClass());

	private boolean haveData = false;

	public boolean isHaveData() {
		return haveData;
	}

	public void setHaveData(boolean haveData) {
		this.haveData = haveData;
	}

	public String getFeedbackKeyAndValue() {
		return feedbackKeyAndValue;
	}

	public void setFeedbackKeyAndValue(String feedbackKeyAndValue) {
		this.feedbackKeyAndValue = feedbackKeyAndValue;
	}

	public String getTableName() {
		return tableName;
	}

	public void setTableName(String tableName) {
		this.tableName = tableName;
	}

	public String getId() {
		return id;
	}

	public void setId(String id) {
		this.id = id;
	}

	public String execute() {
		String tfeedbackKeyAndValue = feedbackKeyAndValue.substring(1,
				feedbackKeyAndValue.length() - 1);
		if (log.isDebugEnabled()) {
			log.debug(tfeedbackKeyAndValue);
			System.out.println(tfeedbackKeyAndValue);
		}
		String[] arrays = tfeedbackKeyAndValue.split("::::::::::");
		StringBuilder sb = new StringBuilder();

		StringBuilder sbId = new StringBuilder();
		if (arrays.length > 1) {//has column
			String tempId = arrays[0];
			System.out.println(tempId);
			String[] tempIds = tempId.split(";;;;;");
			tempId = tempIds[1];
			boolean iflag = true;
			if (!(LightsoftStringUtils.nullOrBlank(tempId))) {// insert
				iflag = false;
			}
			if (iflag) {
				insertInit(sb);
			} else {
				if (haveData) {
					updateInit(sb, sbId);
				} else {
					insertInit(sb);
				}
			}
			if (iflag) {
				insertSql(arrays, sb);
			} else {
				if (haveData) {
					mergeUpdateSql(arrays, sbId, sb);
				} else {
					insertSql(arrays, sb);
				}
			}
		}
		if (log.isDebugEnabled()) {
			log.debug(sb);
			System.out.println(sb);
		}
		return sb.toString();
	}

	private void updateInit(StringBuilder sb, StringBuilder sbId) {
		sb.append("update   ");
		sb.append(tableName);
		sb.append(" set ");
		sbId.append(" where ID='");
		sbId.append(id);
		sbId.append("' ");
	}

	private void insertInit(StringBuilder sb) {
		sb.append("insert into ");
		sb.append(tableName);
		sb.append(" (");
	}

	private void insertSql(String[] arrays, StringBuilder sb) {
		StringBuilder sbValue = new StringBuilder();
		StringBuilder sbColumn = new StringBuilder();
		sbColumn.append("ID");
		sbValue.append("'").append(id).append("'");
		mergeInsertSql(arrays, sbValue, sbColumn, sb);
	}

	private void mergeUpdateSql(String[] arrays, StringBuilder sbId,
			StringBuilder sb) {

		for (int i = 1; i < arrays.length; i++) {
			String temp = arrays[i];
			String[] tempArray = temp.split(";;;;;");
			sb.append(" ").append(tempArray[1]);
			if (log.isDebugEnabled()) {
				log.debug(temp);
				System.out.println(temp);
				for (String string : tempArray) {
					log.debug(string);
					System.out.println(string);
				}
			}

			if (tempArray.length < 3) {
				sb.append("='").append("',");
			} else {
				sb.append("='").append(tempArray[2]).append("',");
			}
		}
		String temp = sb.substring(0, sb.length() - 1);
		sb.delete(0, sb.length());
		sb.append(temp);
		sb.append(" ");
		sb.append(sbId);
	}

	private void mergeInsertSql(String[] arrays, StringBuilder sbValue,
			StringBuilder sbColumn, StringBuilder sb) {

		for (int i = 1; i < arrays.length; i++) {
			String temp = arrays[i];
			String[] tempArray = temp.split(";;;;;");
			if (log.isDebugEnabled()) {
				log.debug(temp);
				System.out.println(temp);
				for (String string : tempArray) {
					log.debug(string);
					System.out.println(string);
				}
			}
			sbColumn.append(",").append(tempArray[1]);
			if (tempArray.length < 3) {
				sbValue.append(",'").append("'");
			} else {
				sbValue.append(",'").append(tempArray[2]).append("'");
			}
		}
		sb.append(sbColumn);
		sb.append(")values(");
		sb.append(sbValue);
		sb.append(")");
	}

	public static void main(String[] args) {
		String feedbackKeyAndValue = "{ID;;;;;::::::::::string;;;;;Test1;;;;;::::::::::string;;;;;Test2;;;;;::::::::::string;;;;;Test3;;;;;}";

		AnalyseCommonDataExt ext = new AnalyseCommonDataExt();
		ext.setTableName("t_customer_ext");
		ext.setFeedbackKeyAndValue(feedbackKeyAndValue);
		ext.setId("11212");
		ext.execute();
	}
}
